<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <div style="margin-top: 15px">
      <el-input
        placeholder="请输入要添加的分类，左边选择分类的等级"
        v-model="input"
        class="input-with-select blogAddInput"
      >
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="1" value="1"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="5" value="5"></el-option>
        </el-select>
      </el-input>
      <el-button type="primary" style="margin-left: 10px" @click="handleAdd"
        >添加</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      element-loading-text="加载中"
      :data="tableData"
      style="width: 100%"
      border
      stripe
      size="mini"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="序号" width="60">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="name" label="分类名称" width="150">
      </el-table-column>
      <el-table-column prop="articleCount" label="文章数量"> </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            plain
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            plain
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑" :visible.sync="dialogFormVisible" top="5vh">
      <el-form :model="form">
        <el-form-item label="分类名称">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序等级">
          <el-select v-model="form.order">
            <el-option label="1" value="1"></el-option>
            <el-option label="2" value="2"></el-option>
            <el-option label="3" value="3"></el-option>
            <el-option label="4" value="4"></el-option>
            <el-option label="5" value="5"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addBlogType,
  deleteBlogType,
  getBlogType,
  getOneBlogType,
  updateOneBlogType
} from '@/api/blogType'
export default {
  data() {
    return {
      input: '',
      select: '1',
      listLoading: false,
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: '',
        name: '',
        order: ''
      }
    }
  },

  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const resp = await getBlogType()
      this.tableData = resp.data
      this.listLoading = false
    },

    async handleAdd() {
      if (this.input) {
        await addBlogType({ name: this.input, order: this.select })
        await this.fetchData()
        this.input = ''
        this.$message.success('添加成功')
      } else {
        this.$message.error('分类名称不能为空')
      }
    },
    async handleEdit(row) {
      const resp = await getOneBlogType(row.id)
      this.form = resp.data
      this.dialogFormVisible = true
    },
    async handleEditSubmit() {
      await updateOneBlogType({
        id: this.form.id,
        data: this.form
      })
      await this.fetchData()
      this.$message.success('编辑成功')
      this.dialogFormVisible = false
    },
    handleDelete(row) {
      this.$confirm(
        '删除该分类后，该分类下所有文章将变为未分类状态，是否继续?',
        '是否删除此文章分类',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(async () => {
          await deleteBlogType(row.id)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.blogAddInput {
  width: 30vw;
  margin-bottom: 20px;
}
</style>
